package filter;

import service.UserService;
import service.impl.UserServiceImpl;
import utils.XBUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebFilter("/*")
public class LoginFilter implements Filter {

    private UserService userService = new UserServiceImpl();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        // 获取请求URI
        String uri =req.getRequestURI();
        //获取请求的资源名称
        String targetName = uri.substring(uri.lastIndexOf("/") + 1);
        //放行
        if(uri.startsWith("/bootstrap") ||
                uri.startsWith("/css") ||
                uri.startsWith("/fonts") ||
                uri.startsWith("/img") ||
                uri.startsWith("/js") ||
                uri.startsWith("/upload") ||
                uri.startsWith("/vendor") ||
                uri.startsWith("/view") ||
                uri.startsWith("/login") ||
                uri.startsWith("/article") ||
                uri.startsWith("/dept") ||
                uri.startsWith("/meeting") ||
                uri.startsWith("/user") ||
                targetName.equals("checkCode.jsp") ||
                targetName.equals("forget.jsp") ||
                targetName.equals("index.jsp") ||
                targetName.equals("taglib.jsp") ||
                targetName.equals("register.jsp") ||
                targetName.equals("login") ||
                targetName.equals("logout") ||
                targetName.equals("forget") ||
                targetName.equals("register") ||
                targetName.equals("sendEmail") ||
                targetName.equals("checkLoginCode") ||
                targetName.equals("checkForgetCode") ||
                targetName.equals("checkUsername") ||
                targetName.equals("checkEmail") ){

            filterChain.doFilter(req, resp);
            return;
        }
        //受限
        HttpSession session = req.getSession();
        if(session.getAttribute("loginUser") != null){
            filterChain.doFilter(req, resp);
            return;
        }
        Cookie loginCookie = XBUtils.getCookie(req, "loginUser");
        if (loginCookie != null) {
            Integer id = Integer.valueOf(loginCookie.getValue());
            session.setAttribute("loginUser", userService.findById(id));
            filterChain.doFilter(req, resp);
            return;
        }

        resp.sendRedirect(req.getContextPath() + "/index.jsp");
    }

    @Override
    public void destroy() {

    }
}
